package com.example.androidkt.fifth

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.androidkt.R
import com.example.androidkt.databinding.ItemDataBindingBinding

class DataBindingAdapater : RecyclerView.Adapter<DataBindingAdapater.DataBindingViewHolder>() {

    private val mList = mutableListOf<ItemBean>()

    init {
        for (i in 0..5) {
            mList.add(ItemBean(i, ("明细$i")))
        }
    }

    class DataBindingViewHolder(private val binding: ItemDataBindingBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(bean: ItemBean) {
            binding.info = bean
            binding.executePendingBindings()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBindingViewHolder {
        return DataBindingViewHolder(
            ItemDataBindingBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )

    }


    override fun getItemCount(): Int = mList.size

    override fun onBindViewHolder(holder: DataBindingViewHolder, position: Int) {
        holder.bind(mList[position])
    }
}